package com.bua.control;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import android.widget.*;
import com.bua.net.Tool;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Color;
import android.util.Base64;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

public class GudingPaint extends LinearLayout {

	private GridView toothGallery;
	private HashMap<Integer, GudingButton> tooths = new HashMap<Integer, GudingButton>();

	public void CreateGDTooth(Context context, int tag, LinearLayout ll,
			OnToothClick l) {
		GudingButton button = new GudingButton(context, tag);
		button.setOnClickListener(l);
		this.tooths.put(tag, button);
		ll.addView(button);
	}

	public GudingPaint(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setOrientation(VERTICAL);
		this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		LinearLayout ShangBuLayout = new LinearLayout(context);
		ShangBuLayout.setOrientation(HORIZONTAL);
		ShangBuLayout.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
		this.addView(ShangBuLayout);
		LinearLayout XiaBuLayout = new LinearLayout(context);
		XiaBuLayout.setOrientation(HORIZONTAL);
		XiaBuLayout.setGravity(Gravity.CENTER_HORIZONTAL);
		this.addView(XiaBuLayout);
		OnToothClick onToothClick = new OnToothClick();
		for (int index = 27; index >= 21; index--) {
			CreateGDTooth(context, index, ShangBuLayout, onToothClick);
		}
		for (int index = 11; index <= 17; index++) {
			CreateGDTooth(context, index, ShangBuLayout, onToothClick);
		}
		for (int index = 37; index >= 31; index--) {
			CreateGDTooth(context, index, XiaBuLayout, onToothClick);

		}
		for (int index = 41; index <= 47; index++) {
			CreateGDTooth(context, index, XiaBuLayout, onToothClick);
		}
		LinearLayout GalleryBuLayout = new LinearLayout(context);
		GalleryBuLayout.setOrientation(HORIZONTAL);
		LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		p.setMargins(0, 30, 0, 0);
		GalleryBuLayout.setLayoutParams(p);
		
		toothGallery = new GridView(context);
		toothGallery.setBackgroundColor(Color.LTGRAY);
		//toothGallery.
		toothGallery.setMinimumHeight(150);
		toothGallery.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		GalleryBuLayout.addView(toothGallery);
		this.addView(GalleryBuLayout);

	}

	public String getPaint() {
		this.setDrawingCacheEnabled(true);
		Bitmap bmPicture = this.getDrawingCache();
		ByteArrayOutputStream pictureOutput = new ByteArrayOutputStream();
		if (bmPicture != null
				&& bmPicture.compress(CompressFormat.JPEG, 50, pictureOutput))
			return Base64.encodeToString(pictureOutput.toByteArray(),
					Base64.DEFAULT);
		return null;
	}

	class OnToothClick implements OnClickListener {

		int tag = 0;

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			tag = (Integer) v.getTag();
			toothGallery.setAdapter(new GallaryAdpter(GudingPaint.this
					.getContext(), tag));
		}

	}

	class GallaryAdpter extends BaseAdapter {

		private Context mContext = null;
		private int mTag;
		private ArrayList<Bitmap> mImageList = new ArrayList<Bitmap>();
		private String[] mTypeList = { "n", "t", "g" };

		public GallaryAdpter(Context context, int tag) {
			super();
			mContext = context;
			mTag = tag;
			Bitmap bm = BitmapFactory.decodeStream(Tool.getFile(mContext, "gd",
					"t" + String.valueOf(mTag), "png"));
			Bitmap dstBitmap = bm.copy(Config.ARGB_8888, true);
			dstBitmap.eraseColor(Color.WHITE);

			mImageList.add(dstBitmap);
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"gd", "t" + String.valueOf(mTag), "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"gd", "g" + String.valueOf(mTag), "png")));
			// mSetter = imgSetter;
			GudingPaint.this.toothGallery.setPadding(30, 5, 30, 5);
		}

		@Override
		public int getCount() {
			// TODO 自动生成的方法存根
			return 3;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO 自动生成的方法存根
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO 自动生成的方法存根
			return arg0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			// TODO 自动生成的方法存根
			ImageButton btnImageItem = new ImageButton(mContext);
			btnImageItem.setImageBitmap(mImageList.get(arg0));
			btnImageItem.setBackgroundColor(Color.TRANSPARENT);
			btnImageItem.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO 自动生成的方法存根

					GudingButton button = GudingPaint.this.tooths.get(mTag);
					button.SetTooth(mTypeList[arg0]);
				}
			});
			return btnImageItem;
		}
	}
}
